package com.piesat.quake.business.device.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.piesat.quake.business.device.mapper.DPassagewayMapper;
import com.piesat.quake.business.device.model.config.Passageway;
import com.piesat.quake.business.device.service.DPassagewayService;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ${author}
 * @since 2023-03-13
 */
@Service
public class DPassagewayServiceImpl extends ServiceImpl<DPassagewayMapper, Passageway> implements DPassagewayService {

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void updatePassageways(List<Passageway> passageways, String id) {
        for (Passageway passageway : passageways) {
            passageway.setId(id);
            QueryWrapper<Passageway> updateWrapper = new QueryWrapper<>();
            updateWrapper.eq("id", id)
                    .eq("passageway_id", passageway.getPassagewayId());
            int update = baseMapper.update(passageway, updateWrapper);
            if (update == 0) {
                int insert = baseMapper.insert(passageway);
                if (insert == 0) {
                    throw new IllegalStateException("通道数据更新失败");
                }
            }
        }
    }
}
